#include "playlist.h"

#include <QDebug>

PlayList::PlayList(QWidget *parent):
    QListWidget(parent)
{
    Init();
}

PlayList::PlayList(QIcon *icon, QWidget *parent)
{
    Init();
    setPlayIcon(icon);
}

void PlayList::addFile(QString str)
{
    QFileInfo fileInfo(str);
    if(fileInfo.suffix() == QString("tpl"))
    {

    }
    map->insert(fileInfo.baseName(), fileInfo.absoluteFilePath());
    QListWidget::addItem(fileInfo.baseName());
    setSelectionMode(QAbstractItemView::ContiguousSelection);
}


void PlayList::addFiles(QStringList *list)
{
    for(int i = 0; i < list->size(); i++)
    {
        addFile(list->at(i));
    }
}


void PlayList::setPlayIcon(QIcon *icon)
{
    delete playIcon;
    playIcon = new QIcon(*icon);

}


void PlayList::Init()
{
    map = new QMap<QString, QString>;
    setDragEnabled(true);
    setDragDropMode(QAbstractItemView::InternalMove);

    connect(this, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(itemActivated(QListWidgetItem*)));

    playIcon = NULL;
    actualSong = QString();
}

QString PlayList::getSong()
{
    if(!count())
    {
        actualSong = QString();
        return actualSong;
    }

    if(actualSong == QString())
    {
        QListWidgetItem *item = this->item(0);
        actualSong = item->text();
        item->setIcon(*playIcon);
        return map->value(actualSong);
    }

    return map->value(actualSong);
}

QString PlayList::nextSong()
{
    if(!count())
    {
        actualSong = QString();
        return actualSong;
    }

    if(actualSong == QString())
    {
        QListWidgetItem *item = this->item(0);
        actualSong = item->text();
        item->setIcon(*playIcon);
        return map->value(actualSong);
    }


    QList<QListWidgetItem*> list = findItems(actualSong, Qt::MatchExactly);
    if(list.empty())
    {
        QListWidgetItem *item = this->item(0);
        actualSong = item->text();
        item->setIcon(*playIcon);
        return map->value(actualSong);
    }

    for(int i = 0; i < list.size(); i++)
    {
        QListWidgetItem *item = list.at(i);
        if(!(item->icon().isNull()))
        {
            int row = this->row(item);
            item->setIcon(QIcon());
            if(this->count()-1 > row)
            {

                item = this->item(row+1);
                actualSong = item->text();
                item->setIcon(*playIcon);
                return map->value(actualSong);
            }
            actualSong = QString();
            return map->value(actualSong);
        }
    }
    return QString();
}

QString PlayList::previousSong()
{
    if(!count())
    {
        actualSong = QString();
        return actualSong;
    }

    if(actualSong == QString())
    {
        QListWidgetItem *item = this->item(0);
        actualSong = item->text();
        item->setIcon(*playIcon);
        return map->value(actualSong);
    }


    QList<QListWidgetItem*> list = findItems(actualSong, Qt::MatchExactly);
    if(list.empty())
    {
        QListWidgetItem *item = this->item(0);
        actualSong = item->text();
        item->setIcon(*playIcon);
        return map->value(actualSong);
    }

    for(int i = 0; i < list.size(); i++)
    {
        QListWidgetItem *item = list.at(i);
        if(!(item->icon().isNull()))
        {
            int row = this->row(item);
            item->setIcon(QIcon());
            if(0 < row)
            {

                item = this->item(row-1);
                actualSong = item->text();
                item->setIcon(*playIcon);
                return map->value(actualSong);
            }
            actualSong = QString();
            return map->value(actualSong);
        }
    }
    return QString();
}

void PlayList::itemActivated(QListWidgetItem *item)
{
    QList<QListWidgetItem*> list = findItems(actualSong, Qt::MatchExactly);

    for(int i = 0; i<list.size(); i++)
    {
        QListWidgetItem *item2 = list.at(i);
        if(!(item2->icon().isNull()))
        {
            item2->setIcon(QIcon());
        }
    }
    item->setIcon(*playIcon);
    actualSong = item->text();
    emit songActivated(QString(map->value(actualSong)));
}

PlayList::~PlayList()
{
    delete map;
    delete playIcon;
}

void PlayList::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Delete)
    {
        removeSongs();
    }
}

void PlayList::removeSongs()
{
    QList<QListWidgetItem*> list = selectedItems();
    for(int i = 0; i < list.size(); i++)
    {
        QListWidget::removeItemWidget(list.at(i));
        map->remove(list.at(i)->text());
        delete list.at(i);
    }
}

bool PlayList::savePlayList(QString filePath)
{
    QFile file(filePath);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        return false;
    }
    for(int i = 0; i < this->count(); i++)
    {
        QTextStream out(&file);
        qDebug(item(i)->text().toStdString().c_str());
        qDebug(map->value(item(i)->text()).toStdString().c_str());
        out << map->value(item(i)->text())<<"\n";
    }
    return true;

}

bool PlayList::openPlayList(QString filePath)
{
    QFile file(filePath);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return false;
    }
    QTextStream in(&file);
    while(!in.atEnd())
    {
        addFile(in.readLine());
    }
    return true;
}

void PlayList::clear()
{
    map->clear();
    QListWidget::clear();
}
